----------------------------
-- Simplistic Street
-- by Jammie Dodger
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

DOORCOST = 30
REFUND = 15

function RemoveFromDoor(door)
	door.Owners = nil
if door.CoOwners then
	door.CoOwners = nil
end

	local rp = RecipientFilter()
	rp:AddAllPlayers()
	
	umsg.Start("EmptyOwners", rp)
		umsg.Entity(door)
	umsg.End()

	door:SetNWBool( "Owned", false )
	door:SetNWString( "doorname", "" )
end

function AddToDoor(door, ply)
	door.Owners = ply:UniqueID()
if door.CoOwners then
	door.CoOwners = nil
end

	local rp = RecipientFilter()
	rp:AddAllPlayers()
	
	umsg.Start("EmptyOwners", rp)
		umsg.Entity(door)
	umsg.End()

	door:SetNWString( "doorname", "Owned by " .. ply:Nick() .. "!" )
	door:SetNWBool( "Owned", true )	
end

function SaveDoors()

	for k,v in pairs(ents.GetAll()) do
		if v:IsDoor() then
			if v:GetNWInt("doorcost") != nil or 0 then
			file.Write("SS/Doors/"..v:EntIndex()..".txt", v:GetNWInt("doorcost"))
			end
			
			LoadDoors()
		end
	end
end

function LoadDoors()

	for k,v in pairs(ents.GetAll()) do
		if v:IsDoor() then
			if file.Exists("SS/Doors/"..v:EntIndex()..".txt") then
			v:SetNWInt("doorcost", file.Read("SS/Doors/"..v:EntIndex()..".txt"))
			
			end
		end
	end
end

function ChangeDoorData(ply, cmd, args)
	local trace = util.GetPlayerTrace(ply)
	local tr = util.TraceLine(trace)

	if !ply:IsSuperAdmin() then return end
	if !args[1] then return end
	
	if ( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then 
	
	tr.Entity:SetNWInt("doorcost", args[1])
	SaveDoors()
	
	end
end

concommand.Add("DoorCost", ChangeDoorData)

function GM:ShowSpare2( ply )
	local rp = RecipientFilter()
	rp:AddAllPlayers()
	local trace = util.GetPlayerTrace(ply)
	local tr = util.TraceLine(trace)
	
	if ( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
		if tr.Entity:IsUnownable() then return end
		if tr.Entity:IsCopOnly() then return end
		
		if ( tr.Entity.Owners == nil ) then
			if ply:CanAfford( ZeroFix( tr.Entity:GetNWInt("doorcost"), DOORCOST ) ) then
				AddToDoor(tr.Entity, ply)
				umsg.Start("ReceiveOwners", rp)
					umsg.Entity(tr.Entity)
					umsg.Entity(ply)
				umsg.End()			
				ply:TakeMoney( ZeroFix( tr.Entity:GetNWInt("doorcost"), DOORCOST ) )
			else
				ply:SendLua("GAMEMODE:AddNotify(\"You cannot afford $"..DOORCOST.."!\", NOTIFY_ERROR, 5)")
			end
		elseif ( tr.Entity.Owners == ply:UniqueID() ) then
			RemoveFromDoor(tr.Entity)
				umsg.Start("ReceiveOwners", rp)
					umsg.Entity(tr.Entity)
					umsg.Entity()
				umsg.End()	
			ply:GiveMoney( REFUND )
		end
	end
end

function GM:ShowSpare1( ply )
	local trace = util.GetPlayerTrace(ply)
	local tr = util.TraceLine(trace)
	
	if ( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
			if tr.Entity.Owners == ply:UniqueID() then
				ply:ConCommand("ss_doormenu")
		
		end
	end
end


local function FindByName(str) for _,v in ipairs(player.GetAll()) do if string.find(string.lower(v:Name()), string.lower(str)) ~= nil then return v end end return nil end

function SendCoOwnersToClient(Ent, Player, Add)
	local rp = RecipientFilter()
	rp:AddAllPlayers()
	
	umsg.Start("CoOwners", rp)
		umsg.Entity(Ent)
		umsg.Entity(Add)
	umsg.End()
end

function FindPlayerUnique(info)

	local pls = player.GetAll()

	for k, v in pairs(pls) do
		if info == v:UniqueID() then
			return v
		end
	end
end

function AddCoOwner( ply, cmd, args )

	local trace = util.GetPlayerTrace(ply)
	local tr = util.TraceLine(trace)
	local UniqueID = args[1]
	local Name = args[2]

	if tr.Entity.CoOwners == nil or "" then
	tr.Entity.CoOwners = {}
	end
	
			if UniqueID == ply:UniqueID() then chat.AddText(ply, Color(255, 0, 0, 255), "You cannot add yourself as a co-owner." ) return end
			if tr.Entity.CoOwners and table.HasValue(tr.Entity.CoOwners, UniqueID) then chat.AddText(ply, Color(255, 0, 0, 255), Name.." is already a co-owner!" ) return end 

				table.insert(tr.Entity.CoOwners, UniqueID)
				SendCoOwnersToClient(tr.Entity, ply, FindPlayerUnique(UniqueID))
				chat.AddText(ply, Color(100,149,237,255), "You added "..Name.." to your door.")
end

concommand.Add("ss_addcoowner", AddCoOwner)

function RemoveCoOwner( ply, cmd, args )

	local trace = util.GetPlayerTrace(ply)
	local tr = util.TraceLine(trace)
	local UniqueID = args[1]
	local Name = args[2]

			if UniqueID == ply:UniqueID() then chat.AddText(ply, Color(255, 0, 0, 255), "You cannot remove yourself from being a co-owner." ) return end
			if tr.Entity.CoOwners and !table.HasValue(tr.Entity.CoOwners, UniqueID) then chat.AddText(ply, Color(255, 0, 0, 255), Name.." is not a co-owner!" ) return end 

				table.remove(tr.Entity.CoOwners, UniqueID)
				chat.AddText(ply, Color(100,149,237,255), "You removed "..Name.." from your door.")
end

concommand.Add("ss_removecoowner", RemoveCoOwner)